package com.snailxr.base.task;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snailxr.base.support.handler.BSZNHandler;
import com.snailxr.base.support.handler.XZSPHandler;
import com.snailxr.base.support.util.Util;
import com.snailxr.base.task.domain.APIObj;
import com.snailxr.base.task.domain.jx.BSZN;
import com.snailxr.base.task.domain.jx.XZSP;
import com.snailxr.base.task.service.APIService;
import com.snailxr.base.task.service.BSZNService;
import com.snailxr.base.task.service.DataTransferService;
import com.snailxr.base.task.service.XZSPService;
/**
 * 具体需要执行的任务类和方法
 * @author luoshijian
 *
 */
@Component
public class TaskGetDatas {

	public final Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private DataTransferService dataTransferService;
	@Resource(name = "xzspHandler")
	private XZSPHandler xzspHandler;
	@Resource(name = "bsznHandler")
	private BSZNHandler bsznHandler;
	@Autowired
	private XZSPService xzspService;
	@Autowired
	private BSZNService bsznService;
	@Autowired
	private DataTransfer dataTransfer;
	/**
	 * 获取行政审批数据
	 */
	public void getXzspDatas() {
		log.info(" run................行政审批......................" + (new Date()));
		
		//从浪潮数据传输表中获取全部json数据
		JSONArray jsonArr = dataTransferService.findAll(dataTransfer.getXzspDataTransferCollection());
		
		log.info("行政审批总计："+jsonArr.size());
		xzspService.deleteAll();
		log.info("已清空行政审批集合数据");
		XZSP xzsp = null;
		for(int j=0;j<jsonArr.size();j++){
			JSONObject item_json =  (JSONObject) jsonArr.getJSONObject(j);
			xzsp = xzspHandler.handle(item_json);
			xzspService.insert(xzsp);
			//调用wcm服务将数据库数据导入wcm
			String metaDataId = xzspHandler.saveMetaViewData(xzsp);
			log.info("-------------------------------->"+metaDataId);
		}
		log.info("-------------------------------->行政审批数据更新结束！");
	}
	
	/**
	 * 获取办事指南数据
	 */
	public void getBsznDatas() {
		log.info(" run................办事指南......................" + (new Date()));
//		APIObj apiObj = apiService.getByApiId(2);
//		log.debug(apiObj);
//		String result = httpUtil.sendGet(apiObj);
//		try {
//			result = URLDecoder.decode(result,"UTF-8");//解码
//		} catch (UnsupportedEncodingException e) {
//			log.error(e.getMessage());
//			e.printStackTrace();
//		}
		String result="[]";
		log.info("数据文件路径:"+this.getClass().getResource("/").toString().substring(6)+"bszn.txt");
		try {
			result = URLDecoder.decode(Util.readFile2String(this.getClass().getResource("/").toString().substring(6)+"bszn.txt"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			log.error("bszn.txt解码失败");
			e.printStackTrace();
		}
		JSONArray jsonArr = JSONArray.parseArray(result);
		log.info("办事指南总计："+jsonArr.size());
		xzspService.deleteAll();
		log.info("已清空办事指南集合数据");
		BSZN bszn= null;
		for(int j=0;j<jsonArr.size();j++){
			JSONObject item_json =  (JSONObject) jsonArr.getJSONObject(j);
			bszn = bsznHandler.handle(item_json);
			bsznService.insert(bszn);
			//调用wcm服务将数据库数据导入wcm
			String metaDataId = bsznHandler.saveMetaViewData(bszn);
			log.info("-------------------------------->"+metaDataId);
		}
		log.info("-------------------------------->办事指南数据更新结束！");
	}
	
	public static void main(String[] args) {
	}
}
